/*
 * @Author: chenyn 1826650603@qq.com
 * @Date: 2023-11-13 08:51:03
 * @LastEditors: chenyn 1826650603@qq.com
 * @LastEditTime: 2023-11-15 19:19:57
 * @FilePath: \smart-campus-platform-front-vue\src\tools\checkAuth.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import router from '../router'
import store from '../store/index.js';
import Global from '../tools/global'; // 引入global.js
import {
    Message
} from 'element-ui';
import Vue from 'vue';
//是否登录的路由地址
var loginInx = location.href.indexOf('/login?');
//教师培训模块需要登陆
var token = localStorage.getItem('logintoken');
if (!token && in_array(Global.decisionPlatform, Global.loginModule)) {
    Message.error("请先登录")
    setTimeout(() => {
        window.location.href = sessionStorage.getItem('host_addr')
    }, 2000)

}
// 页面刷新初始化数据
if (is_empty(store.getters.isCheckToken) && loginInx === -1) {
    //是否设置路由session
    var routerData = [];
    // console.log('zzzzzzzz', token)
    routerData = Vue.prototype.checkRouter(token);

    //动态添加路由
    router.addRoutes(routerData);
    store.commit('SET_USERINFO', !is_empty(sessionStorage.getItem('user_info_str')) ? JSON.parse(sessionStorage.getItem('user_info_str')) : {});
    //设置全局用户id
    Global.user_id = !is_empty(sessionStorage.getItem('user_info_str')) ? JSON.parse(sessionStorage.getItem('user_info_str')).id : 0;
    Global.user_name = !is_empty(sessionStorage.getItem('user_info_str')) ? JSON.parse(sessionStorage.getItem('user_info_str')).name : '';
}
//路由钩子
router.beforeEach((to, from, next) => {

    if (to.name != 'login') {
        if (store.getters.isCheckToken === null) {
            store.commit('UPDATE_ISCHECKTOKEN', true)
        }
    }
    document.body.scrollTop = 0;
    next()

})
router.afterEach(transition => {

});